//
//  AddressItemView.m
//  JiaoX
//
//  Created by yinqy on 15/1/16.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "AddressItemView.h"
#import "AddressDetail.h"

@interface AddressItemView(){
    UILabel *_nameLabel;
    UILabel *_phoneLabel;
    UILabel *_detailLabel;
    UIButton *_selectImage;
    UIButton *_editBtn;
    UIButton *_deleteBtn;
}

@end
@implementation AddressItemView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _nameLabel = [[UILabel alloc]init];
        [_nameLabel setTextColor:[UIColor blackColor]];
        [_nameLabel setFont:[UIFont systemFontOfSize:NAME_FONT_SIZE]];
        [self addSubview:_nameLabel];
        
        _phoneLabel = [[UILabel alloc]init];
        [_phoneLabel setTextColor:[UIColor blackColor]];
        [_phoneLabel setFont:[UIFont systemFontOfSize:NAME_FONT_SIZE]];
        [self addSubview:_phoneLabel];
        
        _detailLabel = [[UILabel alloc]init];
        [_detailLabel setNumberOfLines:0];
        [_detailLabel setTextColor:JXColor(102, 102, 102)];
        [_detailLabel setFont:[UIFont systemFontOfSize:NAME_FONT_SIZE]];
        [self addSubview:_detailLabel];
        
        _selectImage = [[UIButton alloc]init];
        [_selectImage setHidden:YES];
        [_selectImage setBackgroundImage:[UIImage imageNamed:@"ic_address_selected"] forState:UIControlStateSelected];
        [_selectImage setBackgroundImage:[UIImage imageNamed:@"ic_address_unselected"] forState:UIControlStateNormal];
        [self addSubview:_selectImage];
        
        _editBtn = [[UIButton alloc]init];
        [_editBtn.titleLabel setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:12]]];
        [_editBtn setTitleColor:JXColor(153, 153, 153) forState:UIControlStateNormal];
        [_editBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
        [_editBtn setImage:[UIImage imageNamed:@"ic_address_edit"] forState:UIControlStateNormal];
        [_editBtn setTitle:@"编辑" forState:UIControlStateNormal];
        [_editBtn.layer setBorderColor:JXColor(153, 153, 153).CGColor];
        [_editBtn.layer setBorderWidth:0.5];
        [self addSubview:_editBtn];
        
        _deleteBtn = [[UIButton alloc]init];
        [_deleteBtn.titleLabel setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:12]]];
        [_deleteBtn setTitleColor:JXColor(153, 153, 153) forState:UIControlStateNormal];
        [_deleteBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
        [_deleteBtn setImage:[UIImage imageNamed:@"ic_address_delete"] forState:UIControlStateNormal];
        [_deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
        [_deleteBtn.layer setBorderColor:JXColor(153, 153, 153).CGColor];
        [_deleteBtn.layer setBorderWidth:0.5];
        [self addSubview:_deleteBtn];
    }
    return self;
}

-(void)setAddressFrame:(AddressItemFrame *)addressFrame{
    self.frame = addressFrame.selfFrame;
    AddressDetail *detail = addressFrame.detail;
    
    _nameLabel.frame  = addressFrame.adresNameFrame;
    [_nameLabel setText:detail.addressee];
    
    _phoneLabel.frame = addressFrame.adresPhoneFrame;
    [_phoneLabel setText:detail.phoneCode];
    
    _detailLabel.frame = addressFrame.adresDetailFrame;
    [_detailLabel setText:detail.detailAdres2];
    
    _editBtn.frame = addressFrame.adresEditBtnFrame;
    [_editBtn addTarget:self action:@selector(editBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _deleteBtn.frame = addressFrame.adresDeleteBtnFrame;
    [_deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _selectImage.frame = addressFrame.adresSelectBtnFrame;
    if (detail.defaultAdres) {
        [_selectImage setSelected:YES];
    }else{
        [_selectImage setSelected:NO];
    }
    [_selectImage addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_selectImage setHidden:NO];
}
-(void)editBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(editBtnClick:)]) {
        [_delegate editBtnClick:self.tag];
    }
    
}
-(void)deleteBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(deleteBtnClick:)]) {
        [_delegate deleteBtnClick:self.tag];
    }
    
}
-(void)selectBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(selectBtnClick:index:)]) {
        [_delegate selectBtnClick:btn index:self.tag];
    }
    
}

@end
